package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"week2a/appconfig"
	"week2a/models/global"
)

func InitMysql() {
	err := global.Err
	res := appconfig.AppData.Mysql
	//单例模式
	global.Once.Do(func() {
		dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
			res.Name, res.Pwd, res.Host, res.Port, res.Dbname)
		global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	})

	if err != nil {
		fmt.Println("数据库链接失败", err)
		return
	}
	fmt.Println("数据库链接成功", res)
	//自动迁移
	global.DB.AutoMigrate(&VideoWeek2{})

}
